﻿using System;

namespace SpatialHashLib.Topology.Indexing
{
#if DEBUG
    public partial class SpatialHash<T>
    {
        public event EventHandler<CellEventArgs> CellAdded;
        public event EventHandler<CellEventArgs> CellRemoved;
        public event EventHandler<CellEventArgs> CellSearch;

        void OnCellAdded(int row, int col, double cellSize)
        {
            if (CellAdded != null)
                CellAdded(this, new CellEventArgs(row, col, cellSize));
        }

        void OnCellRemoved(int row, int col, double cellSize)
        {
            if (CellRemoved != null)
                CellRemoved(this, new CellEventArgs(row, col, cellSize));
        }

        void OnCellSearch(int row, int col, double cellSize)
        {
            if (CellSearch != null)
                CellSearch(this, new CellEventArgs(row, col, cellSize));
        }
    }

    public class CellEventArgs : EventArgs, IEquatable<CellEventArgs>
    {
        int row;
        int col;
        double cellSize;

        public CellEventArgs(int row, int col, double cellSize)
        {
            this.row = row;
            this.col = col;
            this.cellSize = cellSize;
        }

        public int Row
        {
            get { return row; }
        }

        public int Col
        {
            get { return col; }
        }

        public double CellSize
        {
            get { return cellSize; }
        }

        public Extent Extent
        {
            get
            {
                Coord p0 = new Coord(col * cellSize, row * cellSize);
                Coord p1 = new Coord(p0.X + cellSize, p0.Y + cellSize);
                return new Extent(p0, p1);
            }
        }

        public override int GetHashCode()
        {
            return new HashFunction()
                .Hash(row)
                .Hash(col)
                .Hash(cellSize)
                .HashCode;
        }

        public bool Equals(CellEventArgs other)
        {
            return
                row == other.row &&
                col == other.col &&
                cellSize == other.cellSize
                ;
        }

        public override string ToString()
        {
            return "(row, col, size: " + row + ", " + col + ", " + cellSize + ")";
        }
    }
#endif
}
